<template>
  <div ref="imageWrapper">
    <a-card title="查询区域" class="search">
      <!-- 查询区域 -->
      <div class="table-page-search-wrapper">
        <a-form-model ref="ruleForm" :model="queryParam" :rules="rules" layout="inline" @keyup.enter.native="onSearch">
          <a-row :gutter="24">
            <a-col :md="6" :sm="8">
              <a-form-model-item label="账套/核算单元" prop="accName">
                <a-input v-model="queryParam.accName" readOnly placeholder="请选择" @click.native="openOrganModel" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <a-form-model-item label="审计时间" prop="arrayTime">
                <a-range-picker v-model="queryParam.arrayTime" format="yyyy-MM-DD" value-format="yyyy-MM-DD"
                  @change="handleDatechange" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="6">
              <a-button type="primary" style="margin-left: 10px; background-color: #000e89; border: 1px solid #000e89"
                @click="ShowItemOfIndicator">指标筛选</a-button>
            </a-col>
            <a-col :md="6" :sm="8">
              <span style="float: right; overflow: hidden" class="table-page-search-submitButtons">
                <a-button type="primary" icon="search" class="yellow_search_btn" @click="onSearch">查询</a-button>
                <a-button icon="reload" class="clear_btn" @click="searchReset">清除条件</a-button>
                <a-button type="danger" :loading="ydLoading" @click="handleToYD(false)" icon="arrow-right"
                  style="margin-left: 18px">转疑点</a-button>
                <a-button v-if="isBackButton" type="primary" style="margin-left: 8px" @click="backUppage">
                  返回
                </a-button>
              </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
    </a-card>
    <a-card v-if="modelData.length != 0" class="img_css" style="margin-top: 20px">
      <div class="Exceed_funds_map">
        <div class="title">
          <a-tooltip overlay-class-name="bgc_tooltip" placement="topRight">
            <template slot="title">
              <span>{{ modeltoop.mxms }}</span>
            </template>
            <span @click="getModelData">
              {{ modeltoop.modelName || '成本占比模型' }}
              <a-icon type="strikethrough" @click.stop="showImg" />
            </span>
          </a-tooltip>
        </div>
        <div class="midline">
          <div class="line_v" />
          <div class="line_h" />
        </div>
        <div class="content_box">
          <div v-for="(item, index) in modelData" :key="index" class="content_item">
            <div class="line_v" />
            <div class="step3_left_left" :style="{ color: item.modifyColor }" :class="{ active: index === activeIndex }"
              @click="getModelItem(index, item)">
              <div class="title">
                {{ item.modelName }}
              </div>
              <div class="count">
                {{ item.extraNum || 0 }}
              </div>
              <a-icon type="caret-down" class="logo" />
            </div>
          </div>
        </div>
      </div>
    </a-card>
    <a-card style="margin-top: 20px">
      <div v-if="flag" style="height: 300px">
        <cbzbPie :data="echartsData" :title="echartsTitle" />
      </div>
      <div v-else>
        <a-table :columns="columns" :data-source="dataSource" row-key="id" :bordered="true" :loading="loading"
          class="antableClass" :pagination="ipagination" @change="handleTableChange">
          <span slot="unitName" slot-scope="text">
            <j-ellipsis :value="text" :length="15" />
          </span>
          <span slot="accName" slot-scope="text">
            <j-ellipsis :value="text" :length="10" />
          </span>
          <span slot="summary" slot-scope="text">
            <j-ellipsis :value="text" :length="12" />
          </span>
          <template v-slot:docDate="text">
            <span>{{ text | TimerFormat }}</span>
          </template>
          <span slot="action" slot-scope="text, record">
            <a style="color: red" @click="handleToYD(record)">转疑点</a>
          </span>
        </a-table>
      </div>
    </a-card>
    <CheckOrganModal ref="departModal" @ok="updateQueryParam" />
    <BusinessChart ref="businesschart" :flowimg="flowimg" />
    <YDModel ref="YDModel" :router-data="$route.path" />
    <ItemsOfIndicator ref="itemsofindicator" @save="editIndicator" />
  </div>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { httpAction, getAction, postAction, deleteAction, putAction } from '@/api/manage'
import CheckOrganModal from '../../../sjxmzygl/yzzx/testcomponent/CheckOrganModal'
import BusinessChart from './BusinessChart'
import ItemsOfIndicator from './ItemsOfIndicator.vue'
import { base64toImg } from '@/mixins/base64toImg'
import YDModel from '@/views/sj/components/YDModel'
import cbzbPie from './cbzbPie.vue'
import columns from './columns'
import jumpConfigMixin from '@/views/sj/sjxmzygl/sjxmss/jumpConfigMixin'
export default {
  components: { CheckOrganModal, BusinessChart, YDModel, cbzbPie, ItemsOfIndicator },
  mixins: [JeecgListMixin, base64toImg, jumpConfigMixin],
  data() {
    return {
      queryParam: {
        accName: '',
        unitId: '',
        startTime: '',
        endTime: '',
        extraStartTime: '',
        extraEndTime: '',
        name: '',
        arrayTime: [],
      },
      superParam: {
        id: '95c255303b1effa0a9c12727a8b16470',
        modelId: '95c255303b1effa0a9c12727a8b16470',
        extraFlag: true,
      },
      flag: false,
      modelData: [],
      echartsTitle: '',
      echartsData: [],
      isBackButton: false,
      url: {
        list: '/sjmx/queryCbzbDetail',
        model: '/dataModel/queryDataModelTemplateByExtra',
        details: '/sjmx/queryCbzb',
        // 查询模型筛选项信息
        indicator: '/sjmx/sjModelFieldConfig/queryModelField',
        // 设置模型筛选项信息
        editIndicator: '/sjmx/sjModelFieldConfig/edit',
        // 查询模型转疑点信息(多选)
        TurnSuspects: '/sjmx/queryModelMsgMulti',
      },
      modeltoop: [],
      activeIndex: null,
      rules: {
        accName: [{ required: true, message: '请选择账套/核算单元' }],
        arrayTime: [{ required: true, message: '请选择审计时间' }],
      },
      columns: columns(),
      disableMixinCreated: true,
    }
  },
  filters: {
    TimerFormat(time) {
      return time.indexOf(' ') ? time.split(' ')[0] : time
    },
  },
  computed: {
    flowimg() {
      const url = `${window._CONFIG['domianURL']}/sys/common/static/${this.modeltoop.mxUrl}`
      console.log('url', url)
      return url
    },
  },
  watch: {
    $route: {
      handler: function (to) {
        if (Object.keys(to.query).length) {
          this.disableMixinCreated = true
          console.log(to.query)
          this.queryParam = Object.assign(this.queryParam, to.query)
          this.queryParam.arrayTime = to.query.arrayTime.split(',')
          this.$nextTick(() => {
            this.onSearch()
          })
        } else if (Object.keys(to.params).length && to.params.from == 'sjxmss') {
          let { accName, unitId } = to.params
          this.queryParam.accName = accName
          this.queryParam.unitId = unitId
          let { endDate, startDate } = JSON.parse(to.params.returnRecord)
          this.queryParam.endTime = endDate
          this.queryParam.startTime = startDate
          this.queryParam.arrayTime = [startDate,endDate]
          this.$nextTick(() => {
            // 这可能存在问题
            this.onSearch()
          })
          this.isBackButton = true
          this.returnRecord = to.params.returnRecord
        }
      },
      immediate: true,
    },
  },
  created() { },
  methods: {
    getModelItem(index, { tableName, id }) {
      this.flag = false
      this.activeIndex = index
      this.queryParam.name = tableName
      this.loadData(1)
      // getAction(this.url.list, params).then((res) => {
      //   this.dataSource = res.result.records || res.result
      // })
    },
    searchReset() {
      this.queryParam = {
        accName: '',
        unitId: '',
      }
      this.modelData = []
      this.dataSource = []
      this.flag = false
    },
    onSearch() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.activeIndex = null
          const params = Object.assign({}, this.queryParam, this.superParam)
          getAction(this.url.model, params).then((res) => {
            if (res.success) {
              const data = res.result.records || res.result
              this.modeltoop = data.shift()
              console.log(this.modeltoop)
              this.modelData = data
              this.getModelData()
            }
            if (res.code === 510) {
              this.$message.warning(res.message)
            }
          })
        }
      })
      console.log(this.queryParam)
    },
    getModelData() {
      this.activeIndex = null
      this.echartsData = []
      const params = Object.assign({}, this.queryParam, this.superParam)
      getAction(this.url.details, params).then((res) => {
        if (res.success) {
          const data = res.result
          data.map((item) => {
            this.echartsData.push({ name: item.name, value: item.num })
          })
          this.flag = true
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
      })
    },
    showImg() {
      this.$refs.businesschart.show()
      //    this.$message.warning('请先配置查询条件')
    },
    openOrganModel() {
      this.$refs.departModal.edit()
    },
    updateQueryParam(data, orgId) {
      console.log(data, 'daat')
      let str1 = ''
      let str2 = ''
      let str3 = ''
      data.map((item) => {
        // TODO 账套～～～
        str1 += item.unitId + ','
        str2 += item.accName + ','
        str3 += item.accCode + ','
      })
      if (str1.length > 0) {
        str1 = str1.substr(0, str1.length - 1)
      }
      if (str2.length > 0) {
        str2 = str2.substr(0, str2.length - 1)
      }
      if (str3.length > 0) {
        str3 = str3.substr(0, str3.length - 1)
      }
      this.queryParam.accCode = str3
      this.queryParam.unitId = str1
      this.queryParam.extraId = str1
      this.queryParam.accName = str2
      this.queryParam.orgId = orgId
    },
    handleToYD(record) {
      if (this.dataSource.length) {
        const { startTime = '', endTime = '' } = this.queryParam
        let ydnr
        const time = `【${startTime}～${endTime}】【成本占比模型】`
        this.addYd(this.queryParam, ydnr, time)
      } else {
        this.$message.error('空数据不能新建疑点')
      }
    },
    handleDatechange(dates) {
      this.queryParam.startTime = dates[0]
      this.queryParam.endTime = dates[1]
      this.queryParam.extraStartTime = dates[0]
      this.queryParam.extraEndTime = dates[1]
    },
    // 展示模型筛选项信息
    ShowItemOfIndicator() {
      this.GetItemOfIndicator()
    },
    // 查询模型筛选项信息
    GetItemOfIndicator() {
      getAction(this.url.indicator, Object.assign({}, this.queryParam, this.superParam)).then((res) => {
        if (res.success) {
          let projectItem = Object.assign({}, JSON.parse(res.result.projectJson))
          this.$refs.itemsofindicator.edit(projectItem)
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    // 保存已经修改的projectJson(子组件返回json串)
    editIndicator(projectJson) {
      putAction(this.url.editIndicator, Object.assign({}, this.queryParam, this.superParam, { projectJson })).then(
        (res) => {
          console.log(res)
        }
      )
    },
  },
}
</script>
<style lang="less">
.bgc_tooltip {
  .ant-tooltip-inner {
    // 这里是框框
    border-radius: 20px;
    color: #333;
    width: 400px;
    padding: 10px 15px;
    background-color: rgba(255, 255, 255, 0.274) !important;
  }

  .ant-tooltip-arrow::before {
    // 这里是小三角形
    background-color: #fff !important;
  }
}
</style>
<style lang="less" scoped>
@import '~@assets/less/tableTheme.less';

.img_css /deep/ .ant-card-body {
  padding-top: 7px;
}

/deep/ .ant-table-thead {
  tr {
    th {
      text-align: center !important;
    }
  }
}

.Exceed_funds_map {
  width: 100%;
  position: relative;

  .title {
    // width: 350px;
    height: 50px;
    text-align: center;
    margin: 0 auto;

    padding-top: 7px;

    span {
      border-bottom-left-radius: 30px;
      border-bottom-right-radius: 30px;
      font-size: 22px;
      color: white;
      padding: 10px 35px 15px;
      background-color: rgb(1, 69, 142);
    }
  }

  .midline {
    margin: 0 auto;
    width: 100%;
    height: 40px;

    .line_v {
      margin: 0 auto;
      width: 4px;
      height: 36px;
      border-left: 2px dotted rgb(1, 69, 142);
    }

    .line_h {
      margin: 0 auto;
      width: 80%;
      border-bottom: 2px dotted rgb(1, 69, 142);
    }
  }

  .content_box {
    width: 100%;
    display: flex;
    justify-content: space-around;

    .content_item {
      display: flex;
      flex-direction: column;
      justify-content: flex-start;

      .line_v {
        margin: 0 auto;
        height: 15px;
        width: 4px;
        border-left: 2px dotted rgb(1, 69, 142);
      }

      display: flex;
      justify-content: space-around;
      align-items: center;

      .step3_left_left {
        // flex-shrink:1;
        width: 200px;
        height: 70px;
        border: 2px solid #000e89;
        font-size: 16px;
        font-weight: 700;
        color: white;
        text-align: center;
        cursor: pointer;

        .title {
          height: 35px;
          line-height: 20px;
          background-color: rgb(1, 69, 142);
        }

        .count {
          line-height: 30px;
          height: 31px;
          color: rgb(1, 69, 142);
        }

        .logo {
          position: relative;
          top: -5px;
          color: #fff;
        }
      }

      .step3_left_left {
        margin-left: 10px;
      }
    }
  }

  .active {
    .title {
      background-color: rgb(236, 149, 54) !important;
      height: 30px;
      padding-bottom: 10px !important;
    }

    .count {
      color: orange !important;
      background-color: rgb(253, 245, 236);
    }

    .logo {
      color: rgb(236, 149, 54) !important;
    }

    border: 2px solid #ec9536 !important;
  }
}
</style>
